package com.mtbu.bean;
import my.myorm.tool.jdbc.ResultList;
import my.myorm.tool.jdbc.SQLExecutor;
import my.myorm.tool.jdbc.SQLManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mtbu.bean.BppCata;
import com.mtbu.bean.BppChannel;
import com.mtbu.bean.BppInfo;
import com.mtbu.dao.DAOManager;
import com.mtbu.bean.base.BppChannelBase;
public final class BppChannel extends BppChannelBase {
	
	Logger logger =  LoggerFactory.getLogger(BppChannel.class);
	
	public BppChannel getPidChannel() {
		
		
		
		BppChannel ch;
		try {
			ch = DAOManager.bppChannelDao.findBppChannel("id",getPID(),"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage());
			ch = null;
		}
		return ch;
	}
	
	public BppInfo getInfo() {
	
		BppInfo info ;
		try {
			info = DAOManager.bppInfoDao.findBppInfo(getInfoID());
			
			if(info == null){
				info = new BppInfo();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage());
			info = new BppInfo();
		}
		
		
		return info;
	}
	
	/*
	 * 合集和频道返回孩子
	 */
	public BppChannel[] getChildChannels() {
		
		BppChannel[] res = null;
	
	
		if (getVT() == 21) {
			//剧集
			String sql="select * from " + "bpp_channel"  + " where " + "pid" + "=?";
		    SQLExecutor dbconn;
			try {
				dbconn = SQLManager.getSQLExecutor("EPGDS",true);
				dbconn.addParam(getId());
				ResultList rs = dbconn.query(sql,false,null);
				if(rs.size()>0){
					res = new BppChannel[rs.size()];
					for(int i=0;i<rs.size();i++){
						res[i] = rs.get(i).get(BppChannel.class);
					}
				}				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		else if (getVT() == 22) {
			//合集
			//String sql="select * from " + "bpp_set_channel"  + " where " + "setid" + "=?";
			String sql ="select * from  bpp_channel bc  inner join bpp_set_channel bsc on bsc.channelid=bc.id where bsc.setid=?";
		    SQLExecutor dbconn;
			try {
				dbconn = SQLManager.getSQLExecutor("EPGDS",true);
				dbconn.addParam(getId());
				ResultList rs = dbconn.query(sql,false,null);
				if(rs.size()>0){
					res = new BppChannel[rs.size()];
					for(int i=0;i<rs.size();i++){
						res[i] = rs.get(i).get(BppChannel.class);
					}
				}				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		
		return res;
	}
	
	public BppCata getCata() throws Exception{
		return DAOManager.bppCataDao.findBppCata(getCataID());
	}
	
	public BppCata getParentCata() throws Exception{
		if (getCata().getPID() == 0) {
			return null;
		}
		return DAOManager.bppCataDao.findBppCata(getCata().getPID());
	}
}
